home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 32 / Amiga Format AFCD32 (Nov 1998, Issue 117).iso / -seriously_amiga- / programming / c / mesa-2.6 / src / mmath.h < prev    next >
C/C++ Source or Header  |  1998-08-10  |  3KB  |  112 lines

  1.  
  2. /*
  3.  * Mesa 3-D graphics library
  4.  * Version:  2.6
  5.  * Copyright (C) 1995-1997  Brian Paul
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Library General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Library General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Library General Public
  18.  * License along with this library; if not, write to the Free
  19.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22.  
  23.  
  24. /*
  25.  * mmath.h
  26.  *
  27.  * Version 1.0  01 Aug 1998
  28.  * by Jarno van der Linden
  29.  * jarno@kcbbs.gen.nz
  30.  *
  31.  * Based on mmath.h ver 1.5
  32.  * Included <m68881.h> for fast sqrt etc.
  33.  * Note that this provides a faster sqrt than gl_sqrt,
  34.  * so you probably won't want to #define FAST_MATH
  35.  *
  36.  *
  37.  */
  38.  
  39.  
  40. /*
  41.  * Faster arithmetic functions.  If the FAST_MATH preprocessor symbol is
  42.  * defined on the command line (-DFAST_MATH) then we'll use some (hopefully)
  43.  * faster functions for sqrt(), etc.
  44.  */
  45.  
  46.  
  47. #ifndef MMATH_H
  48. #define MMATH_H
  49.  
  50.  
  51. #include <math.h>
  52.  
  53. #include <m68881.h>
  54.  
  55. /*
  56.  * Set the x86 FPU control word to less precision for more speed:
  57.  */
  58. #if defined(__linux__) && defined(__i386__) && defined(FAST_MATH)
  59. #include <i386/fpu_control.h>
  60. #define START_FAST_MATH  __setfpucw(_FPU_SINGLE | _FPU_MASK_IM | _FPU_MASK_DM \
  61.             | _FPU_MASK_ZM | _FPU_MASK_OM | _FPU_MASK_UM | _FPU_MASK_PM);
  62. #define END_FAST_MATH  __setfpucw(_FPU_EXTENDED | _FPU_MASK_IM | _FPU_MASK_DM \
  63.             | _FPU_MASK_ZM | _FPU_MASK_OM | _FPU_MASK_UM | _FPU_MASK_PM);
  64. #else
  65. #define START_FAST_MATH
  66. #define END_FAST_MATH
  67. #endif
  68.  
  69.  
  70.  
  71. #if defined(USE_ASM)
  72. static __inline__ int FloatToInt(float f)
  73. {
  74.    int r;
  75.    __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
  76.    return r;
  77. }
  78. #else
  79. #define FloatToInt(F) ((int) (F))
  80. #endif
  81.  
  82.  
  83.  
  84. extern float gl_sqrt(float x);
  85.  
  86.  
  87. #ifdef FAST_MATH
  88. #  define GL_SQRT(X)  gl_sqrt(X)
  89. #else
  90. #  define GL_SQRT(X)  sqrt(X)
  91. #endif
  92.  
  93.  
  94. /* Normalize a 3-element vector to unit length */
  95. #define NORMALIZE_3FV( V )                \
  96. {                            \
  97.    GLfloat len;                        \
  98.    len = GL_SQRT(V[0]*V[0]+V[1]*V[1]+V[2]*V[2]);    \
  99.    if (len>0.0001F) {                    \
  100.       len = 1.0F / len;                    \
  101.       V[0] *= len;                    \
  102.       V[1] *= len;                    \
  103.       V[2] *= len;                    \
  104.    }                            \
  105. }
  106.  
  107.  
  108. extern void gl_init_math(void);
  109.  
  110.  
  111. #endif
  112.